package com.ztools.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.StringRes;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.ztools.utils.NumberUtils;


/**
 * Created by zhangxuehui on 2018/4/4.
 */

public class AutoColorTextView extends AppCompatTextView {
    private boolean adjustTopForAscent = true;
    private int defaultColor;

    public AutoColorTextView(Context context) {
        this(context, null);
    }

    public AutoColorTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        defaultColor = this.getCurrentTextColor();
    }

    Paint.FontMetricsInt fontMetricsInt;

    @Override
    protected void onDraw(Canvas canvas) {
        if (adjustTopForAscent) {//设置是否remove间距，true为remove
            if (fontMetricsInt == null) {
                fontMetricsInt = new Paint.FontMetricsInt();
                getPaint().getFontMetricsInt(fontMetricsInt);
            }
            canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
        }
        super.onDraw(canvas);
    }

    public void setTextWithColor(@StringRes int resid) {
        String str = getResources().getString(resid);
        if (TextUtils.isEmpty(str)) {
            return;
        }
        str = str.trim();
        if (TextUtils.isEmpty(str)) {
            return;
        }
        boolean isNum = NumberUtils.isNumber(str);
        if (isNum) {
            String temp = "";
            if (str.contains("%")) {
                temp = str.replace("%", "");
            }
            if (temp.contains("+")) {
                temp = temp.replace("+", "");
            }
            if (TextUtils.isEmpty(temp)) {
                return;
            }
            if (Double.parseDouble(temp) > 0) {
                this.setTextColor(Color.parseColor("#FFFF5532"));
                this.setText(addJIAHAO(str));
                return;
            } else if (Double.parseDouble(temp) == 0) {
                this.setTextColor(defaultColor);
            } else {
                this.setTextColor(Color.parseColor("#FF36C7A3"));
            }
            this.setText(str);
        } else {
            this.setTextColor(defaultColor);
            this.setText(str);
        }
    }

    public void setTextWithColor(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        str = str.trim();
        if (TextUtils.isEmpty(str)) {
            return;
        }
        boolean isNum = NumberUtils.isNumber(str);
        if (isNum) {
            String temp = str;
            if (str.contains("%")) {
                temp = str.replace("%", "");
            }
            if (temp.contains("+")) {
                temp = temp.replace("+", "");
            }
            if (TextUtils.isEmpty(temp)) {
                return;
            }
            if (Double.parseDouble(temp) > 0) {
                this.setTextColor(Color.parseColor("#FFFF5532"));
                this.setText(addJIAHAO(str));
                return;
            } else if (Double.parseDouble(temp) == 0) {
                this.setTextColor(defaultColor);
            } else {
                this.setTextColor(Color.parseColor("#FF36C7A3"));
            }
            this.setText(str);
        } else {
            this.setTextColor(defaultColor);
            this.setText(str);
        }
    }

    /**
     * 移除加减号
     */
    public void removeSign() {
        String str = this.getText().toString();
        if (!TextUtils.isEmpty(str)) {
            if (str.contains("+")) {
                str = str.replace("+", "");
            } else if (str.contains("-")) {
                str = str.replace("-", "");
            }
            this.setText(str);
        }
    }

    /**
     * 添加➕号
     *
     * @param s
     */
    private String addJIAHAO(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        s = s.trim();
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        if (s.contains("+")) {
            return s;
        } else {
            return "+" + s;
        }
    }
}
